package com.itheima.exception_handler;


import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;
import com.itheima.utils.EmailUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.access.AuthorizationServiceException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author W.J.Y
 */
@Component
@ControllerAdvice
public class DoException {

    /**
     * 统一异常处理类
     *
     * @param e 捕捉到的异常
     * @return 异常处理后的结果
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result doException(Exception e) {
        e.printStackTrace();
        /**
         * 判断是业务异常则直接返回
         */
        if (e instanceof BusinessException) {
            return new Result(false, e.getMessage());
        }
        if (e instanceof AccessDeniedException){
            return new Result(false,"权限不足，操作失败！");
        }
        EmailUtils.sendMail("wujianyong_1994@163.com",e.getMessage(),"出现系统异常，需要处理！！");
        return new Result(false, "未知错误，请联系管理员！");
    }
}
